package internal

import (
	"k9-panel/app/model"
	"k9-panel/app/query"
	"k9-panel/app/service/dto"
)

type HistoryConfigService struct {
}

func (h *HistoryConfigService) Publish(param *dto.HistoryConfigPublishParam) (uint, error) {
	_, err := h.Create(param.ProjectId, param.EnvironmentId, param.Content, param.Memo)
	if err != nil {
		return 0, err
	}
	update, err := query.HistoryConfig.
		Where(query.HistoryConfig.ProjectId.Eq(int(param.ProjectId))).
		Where(query.HistoryConfig.EnvironmentId.Eq(int(param.EnvironmentId))).
		Update(query.HistoryConfig.Status, model.HistoryConfigStatusPublish)
	if err != nil {
		return 0, err
	}
	return uint(update.RowsAffected), nil
}

func (h *HistoryConfigService) List(filter *dto.QueryListHistory) (*dto.RspListHistory, error) {
	//TODO implement me
	var config query.IHistoryConfigDo
	config = query.HistoryConfig.ReadDB()
	if filter == nil {
		filter = &dto.QueryListHistory{
			PageIndex: 1,
			PageSize:  10,
		}
	}

	if filter.ProjectId > 0 {
		config.Where(query.HistoryConfig.ProjectId.Eq(int(filter.ProjectId)))
	}
	if filter.EnvironmentId > 0 {
		config.Where(query.HistoryConfig.EnvironmentId.Eq(int(filter.EnvironmentId)))
	}
	config.Order(query.HistoryConfig.ID.Desc())
	limit := int(10)
	if filter.PageSize != 0 {
		limit = filter.PageSize
	}
	offset := 0
	if filter.PageIndex > 0 {
		offset = filter.PageIndex * limit
	}
	page, total, err := config.FindByPage(offset, limit)
	if err != nil {
		return nil, err
	}

	return &dto.RspListHistory{
		Items: page,
		Total: total,
	}, nil
}

func (h *HistoryConfigService) Get(id uint) (*model.HistoryConfig, error) {
	first, err := query.HistoryConfig.Where(query.HistoryConfig.ID.Eq(id)).First()
	if err != nil {
		return nil, err
	}
	return first, nil
}

func (h *HistoryConfigService) Create(projectId uint, envId uint, content string, memo string) (uint, error) {
	project := &model.Project{}
	project.ID = projectId
	prj, err := svc.Project.Get(project)
	if err != nil || prj == nil {
		return 0, err
	}
	env, err := svc.Environment.Get(envId)
	if err != nil || env == nil {
		return 0, err
	}
	config := &model.HistoryConfig{
		EnvironmentId: int(envId),
		ProjectId:     int(projectId),
		Status:        model.HistoryConfigStatusUnPublish,
		File:          content,
		Memo:          memo,
		DeletedAt:     0,
	}
	err = query.HistoryConfig.Create(config)
	if err != nil {
		return 0, err
	}
	//TODO implement me
	return config.ID, nil
}

func (h *HistoryConfigService) UnPublish(projectId uint, envId uint) (uint, error) {
	update, err := query.HistoryConfig.
		Where(query.HistoryConfig.ProjectId.Eq(int(projectId))).
		Where(query.HistoryConfig.EnvironmentId.Eq(int(envId))).
		Update(query.HistoryConfig.Status, model.HistoryConfigStatusUnPublish)
	if err != nil {
		return 0, err
	}
	return uint(update.RowsAffected), nil
}

func (h *HistoryConfigService) Delete(id uint) (int64, error) {
	info, err := query.HistoryConfig.
		Where(query.DerivedConfig.ID.Eq(id)).Limit(1).Delete()
	if err != nil {
		return 0, err
	}
	return info.RowsAffected, nil
}

func (h *HistoryConfigService) View(envName string, projectName string) (*model.HistoryConfig, error) {
	project := &model.Project{}
	project.Name = projectName
	prj, err := svc.Project.Get(project)
	if err != nil || prj == nil {
		return nil, err
	}
	env, err := svc.Environment.GetByModel(&model.Environment{Name: envName})
	if err != nil || env == nil {
		return nil, err
	}
	first, err := query.HistoryConfig.Where(query.HistoryConfig.ProjectId.Eq(int(prj.ID))).
		Where(query.HistoryConfig.EnvironmentId.Eq(int(env.ID))).
		Where(query.HistoryConfig.Status.Eq(model.HistoryConfigStatusPublish)).
		Order(query.HistoryConfig.ID.Desc()).
		First()
	if err != nil {
		return nil, err
	}
	first.Project = prj
	first.Environment = env
	return first, nil
}
